package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BGreenCarbonSink;
import com.isdepci.project.carbon.domain.BGreenCarbonSinkParameter;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BGreenCarbonSinkMapper;
import com.isdepci.project.carbon.mapper.BGreenCarbonSinkParameterMapper;
import com.isdepci.project.carbon.service.IBGreenCarbonSinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 绿化碳汇Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BGreenCarbonSinkServiceImpl implements IBGreenCarbonSinkService
{
    @Autowired
    private BGreenCarbonSinkMapper bGreenCarbonSinkMapper;

    @Autowired
    private BGreenCarbonSinkParameterMapper carbonSinkParameterMapper;

    @Override
    public BGreenCarbonSinkParameter getGreenCarbonSinkParameters(Integer greeningType) {
        return carbonSinkParameterMapper.findByGreeningType(greeningType);
    }

//    @Override
//    public AjaxResult calGreenCarbon(Long projectId) {
//        // 查询根据 projectId 相关的数据
//        List<BGreenCarbonSink> bGreenCarbonSinks = bGreenCarbonSinkMapper.selectBGreenCarbonSinkByProjectId(projectId);
//
//        // 初始化总的碳汇量
//        BigDecimal totalCarbon = BigDecimal.ZERO;
//        BigDecimal totalGreeningCarbonSink = BigDecimal.ZERO; // 总植绿碳汇量
//        BigDecimal totalDestroyCarbonSink = BigDecimal.ZERO;  // 总毁绿碳汇量
//        // 遍历数据进行计算
//        for (BGreenCarbonSink sink : bGreenCarbonSinks) {
//            // 计算每条数据的植绿碳汇量
//            BigDecimal greeningCarbonSink = sink.getGreeningArea()
//                    .multiply(sink.getAbsorptionFactor())
//                    .multiply(BigDecimal.valueOf(sink.getGreeningMonths()))
//                    .multiply(BigDecimal.valueOf(1e-3)); // 10的负三次方
//
//            // 计算每条数据的毁绿碳汇量
//            BigDecimal destroyCarbonSink = sink.getDestroyArea()
//                    .multiply(sink.getAbsorptionFactor())
//                    .multiply(BigDecimal.valueOf(sink.getDestroyMonths()))
//                    .multiply(BigDecimal.valueOf(1e-3)); // 10的负三次方
//
//            // 累加植绿碳汇量和毁绿碳汇量
//            totalGreeningCarbonSink = totalGreeningCarbonSink.add(greeningCarbonSink);
//            totalDestroyCarbonSink = totalDestroyCarbonSink.add(destroyCarbonSink);
//            // 计算绿化碳汇量
//            BigDecimal greeningOffsetCarbon = totalGreeningCarbonSink.subtract(totalDestroyCarbonSink);
//            sink.setCarbonEmissions(greeningOffsetCarbon);
//            bGreenCarbonSinkMapper.updateBGreenCarbonSink(sink);
//
//            totalCarbon = totalCarbon .add(greeningOffsetCarbon);
//        }
//        // 返回计算结果
//        return AjaxResult.success(totalCarbon.setScale(2, RoundingMode.HALF_UP));
//    }


    @Override
    public AjaxResult calGreenCarbon(Long projectId) {
        // 查询根据 projectId 相关的数据
        List<BGreenCarbonSink> bGreenCarbonSinks = bGreenCarbonSinkMapper.selectBGreenCarbonSinkByProjectId(projectId);

        // 遍历数据进行计算
        for (BGreenCarbonSink sink : bGreenCarbonSinks) {
            // 计算每条数据的植绿碳汇量
            BigDecimal greeningCarbonSink = sink.getGreeningArea()
                    .multiply(sink.getAbsorptionFactor())
                    .multiply(BigDecimal.valueOf(sink.getGreeningMonths()))
                    .multiply(BigDecimal.valueOf(1e-3)); // 10的负三次方

            // 计算每条数据的毁绿碳汇量
            BigDecimal destroyCarbonSink = sink.getDestroyArea()
                    .multiply(sink.getAbsorptionFactor())
                    .multiply(BigDecimal.valueOf(sink.getDestroyMonths()))
                    .multiply(BigDecimal.valueOf(1e-3)); // 10的负三次方

            // 计算绿化碳汇量
            BigDecimal greeningOffsetCarbon = greeningCarbonSink.subtract(destroyCarbonSink);
            sink.setCarbonEmissions(greeningOffsetCarbon);

            // 更新数据库中的碳排放值
            bGreenCarbonSinkMapper.updateBGreenCarbonSink(sink);
        }

        // 返回成功的响应
        return AjaxResult.success("计算完成");
    }





    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BGreenCarbonSink> list =  excel.getGreenCarbonSinkList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bGreenCarbonSinkMapper.insertList(list);
        }
        return AjaxResult.success();
    }


    /**
     * 查询绿化碳汇
     */
    @Override
    public BGreenCarbonSink selectBGreenCarbonSinkById(Long id)
    {
        return bGreenCarbonSinkMapper.selectBGreenCarbonSinkById(id);
    }

    /**
     * 查询绿化碳汇列表
     */
    @Override
    public List<BGreenCarbonSink> selectBGreenCarbonSinkList(BGreenCarbonSink bGreenCarbonSink)
    {
        return bGreenCarbonSinkMapper.selectBGreenCarbonSinkList(bGreenCarbonSink);
    }

    /**
     * 新增绿化碳汇
     */
    @Override
    public int insertBGreenCarbonSink(BGreenCarbonSink bGreenCarbonSink)
    {
        return bGreenCarbonSinkMapper.insertBGreenCarbonSink(bGreenCarbonSink);
    }

    /**
     * 修改绿化碳汇
     */
    @Override
    public int updateBGreenCarbonSink(BGreenCarbonSink bGreenCarbonSink)
    {
        return bGreenCarbonSinkMapper.updateBGreenCarbonSink(bGreenCarbonSink);
    }

    /**
     * 批量删除绿化碳汇
     */
    @Override
    public int deleteBGreenCarbonSinkByIds(Long[] ids)
    {
        return bGreenCarbonSinkMapper.deleteBGreenCarbonSinkByIds(ids);
    }

    /**
     * 删除绿化碳汇信息
     */
    @Override
    public int deleteBGreenCarbonSinkById(Long id)
    {
        return bGreenCarbonSinkMapper.deleteBGreenCarbonSinkById(id);
    }


}
